package jdbc;/*
 *
 * @author 陈粒
 * @version 创建时间：2021/8/4 9:00
 */

import util.JDBCutils;

import java.sql.*;
import java.util.Scanner;

public class JDBC_NewNewLogin {
    public static void main(String[] args) {
        System.out.println("请输入用户名：");
        String user = new Scanner(System.in).nextLine();
        System.out.println("请输入密码");
        String password = new Scanner(System.in).nextLine();
        boolean flag = new JDBC_NewNewLogin().login(user, password);
        if (flag == true) {
            System.out.println("登录成功");
        } else {
            System.out.println("登录失败");
        }
    }

    public boolean login(String username, String password) {
        if (username == null || password == null) {
            return false;
        }
        Connection conn = null;
        PreparedStatement pstm = null;
        ResultSet rs = null;
        try {
            conn = JDBCutils.getConnection();
            String sql = "select * from login where name = ? and password = ?";
            pstm = conn.prepareStatement(sql);
            pstm.setString(1, username);
            pstm.setString(2, password);
            rs = pstm.executeQuery();
            return rs.next();

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCutils.close(rs, pstm, conn);
        }
        return false;
    }

}
